package com.cskaoyan.util;


import com.cskaoyan.bean.po.MarketCart;
import com.cskaoyan.bean.po.MarketCartExample;
import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.vo.front.coupon.CouponUserVo;
import com.cskaoyan.mapper.MarketCartMapper;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 优惠券有效
 *
 * @author 李然
 * @date 2022/10/15
 */
@Component
public class IsCouponValid {


    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketCartMapper marketCartMapper;

    private static MarketCouponMapper couponMapper;
    private static MarketCouponUserMapper couponUserMapper;
    private static MarketCartMapper cartMapper;




    @PostConstruct
    public void init() {
        // 通过静态注入的方式，让静态方式使用容器中的组件
        couponMapper = this.marketCouponMapper;

        couponUserMapper = this.marketCouponUserMapper;

        cartMapper = this.marketCartMapper;
    }



    public static boolean judge(MarketCoupon coupon) {
        long nowDate = DateFormatUtil.dateToStamp(new Date());
        if (coupon.getTimeType() != null && coupon.getTimeType() == 0) {
            return true;
        }

        long endTime = DateFormatUtil.dateToStamp(coupon.getEndTime());

        return endTime > nowDate;
    }

    public static boolean judge(MarketCouponUser couponUser) {
        MarketCoupon coupon = new MarketCoupon();
        coupon.setEndTime(couponUser.getEndTime());
        return judge(coupon);
    }


    public static List<CouponUserVo> getAvailableCoupon(List<MarketCouponUser> couponUsers, Integer userId, Integer cartId) {
        List<CouponUserVo> couponUsers1 = new ArrayList<>();
        //筛选过期的
        for (MarketCouponUser couponUser : couponUsers) {
            if (!judge(couponUser)) {
                couponUser.setStatus((short) 2);
                couponUserMapper.updateByPrimaryKeySelective(couponUser);
                break;
            }
            //找到优惠券属性
            MarketCoupon coupon = couponMapper.selectByPrimaryKey(couponUser.getCouponId());
            //优惠券可用开关
            Boolean available = false;
            //获取优惠券的使用条件
            String[] strings = coupon.getGoodsValue();
            List<String> goodsValue = Arrays.asList(strings);
            //根据cartId去查商品信息
            MarketCartExample cartExample = new MarketCartExample();
            MarketCartExample.Criteria criteria = cartExample.createCriteria()
                    .andUserIdEqualTo(userId)
                    .andDeletedEqualTo(false).andCheckedEqualTo(true);

            List<MarketCart> carts = cartMapper.selectByExample(cartExample);
            if (cartId != 0) {
                //criteria.andIdEqualTo(cartId);
                MarketCart cart = cartMapper.selectByPrimaryKey(cartId);
                carts = new ArrayList<>();
                carts.add(cart);
            }

            //先判断使用时间
            if (DateFormatUtil.isStart(couponUser.getStartTime())) {
                //同类总金额
                BigDecimal typeSum = new BigDecimal(0);
                //再判断是否使用该商品
                for (MarketCart cart : carts) {
                    //类目集合
                    if (coupon.getGoodsType() == 1) {
                        //判断优惠券是否支持此类目
                        if (goodsValue.contains(cart.getProductId())) {
                            //判断使用金额限制
                            typeSum = typeSum.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
                            if (typeSum.compareTo(coupon.getMin()) >= 0) {
                                available = true;
                            }
                        }
                        //商品集合
                    } else if (coupon.getGoodsType() == 2) {
                        if (goodsValue.contains(cart.getGoodsId())) {
                            typeSum = typeSum.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
                            if (typeSum.compareTo(coupon.getMin()) >= 0) {
                                available = true;
                            }
                        }
                    } else {

                        typeSum = typeSum.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
                        if (typeSum.compareTo(coupon.getMin()) >= 0) {
                            available = true;
                        }

                    }
                }
            }

            CouponUserVo couponUserVo = new CouponUserVo(available, couponUser.getCouponId(), coupon.getDesc(), coupon.getDiscount(), couponUser.getEndTime(), couponUser.getId(), coupon.getMin(), coupon.getName(), couponUser.getStartTime(), coupon.getTag());
            couponUsers1.add(couponUserVo);
        }
        return couponUsers1;
    }

}
